void solve()
{
  int n, m;
  cin >> n >> m;

  vector<pii> ret(n + 1);
  int mx = 0, val = 0;
  unordered_map<int, int> cc;
  rep(i, 1, n) {
    int l, x;
    cin >> l;

    int mex = 0, but = 0, cnt = 0;
    unordered_map<int, int> f;
    rep(j, 1, l) {
      cin >> x;
      f[x] = 1;
    }

    while (true) {
      if (f.count(mex)) {
        mex++;
      } else {
        cnt++;
        if (cnt >= 2) {
          break;
        } else {
          cc[mex]++;
          val = max(val, mex);
          but = mex;
          mex++;
        }
      }
    }
    ret[i] = {but, mex};
    mx = max(mx, mex);
  }

  vvi g(mx + 2);
  vi dp(mx + 2);
  rep(i, 1, n) {
    g[ret[i].se].pb(ret[i].fi);
  }

  per(i, mx, 0) {
    dp[i] = max(i, dp[i]);
    dp[i] = max(dp[i], val);
    for (auto &j : g[i]) {
      dp[j] = max(dp[j], dp[i]);
    }
  }

  ll ans = 0;

  int res = dp[0], res2 = 0;
  for (auto &[x, y]: cc) {
    if (y >= 2) {
      res = max(res, dp[x]);
      res2 = max(res2, dp[x]);
    } else {
      res = max(res, x);
    }
  }

  rep(i, 0, min(mx, m)) {
    ans += max(res2, dp[i]);
  }

  auto calc = [&](int l, int r) {
    if (l > r) {
      return 0ll;
    }
    return 1ll * (r - l + 1) * (l + r) / 2;
  };
  if (m >= mx + 1) {
    ans += 1ll * max(0, res - mx - 1) * res;
    ans += calc(max(res + 1, mx + 1), m);
  }
  cout << ans << '\n';
}